package com.cykj.management.channel;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cykj.management.common.MyLog;
import com.cykj.management.activity.TtsActivity;

import java.util.Map;

import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.StandardMessageCodec;

/**
 * @author HaoJie
 * @date 2021/06/15 10:40
 */
@Deprecated
public class MscChannel implements BasicMessageChannel.MessageHandler {
    public static final String TAG = MscChannel.class.getSimpleName();
    private static String channelName = "MscChannel";

    private static MscChannel instance;
    private static BasicMessageChannel mscChannel;
    private static Context context;

    public static void init(FlutterEngine flutterEngine) {
        mscChannel = new BasicMessageChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), channelName, StandardMessageCodec.INSTANCE);
    }

    public static MscChannel getInstance() {
        if (instance == null) {
            instance = new MscChannel();
        }
        return instance;
    }

    public void setContext(Context context) {
        this.context = context;
    }


    public static BasicMessageChannel getChannel() {
        return mscChannel;
    }

    @Override
    public void onMessage(@Nullable Object msg, @NonNull BasicMessageChannel.Reply reply) {
        MyLog.d(TAG, "android接收到的消息：" + msg.toString());
        Map map = (Map) msg;
        String method = map.get("method").toString();
        switch (method) {
            case "openGate":
                boolean openResult = (boolean) map.get("openResult");
                TtsActivity ttsActivity = TtsActivity.getInstance();
                ttsActivity.init(context);
                if (openResult) {
                    ttsActivity.tts_play("道闸已开启");
                } else {
                    ttsActivity.tts_play("开闸失败");
                }
                ttsActivity.release();
                break;
            default:
                break;
        }

    }
}
